package com.huage.courseplatform.framework.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.huage.courseplatform.framework.model.param.OrderPARAM;
import com.huage.courseplatform.framework.model.param.PagePARAM;
import com.huage.courseplatform.framework.model.param.SearchPARAM;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

@Slf4j
public class AbstractController<E, D, P> {

    public AbstractController() {
    }

    protected E paramToEntity(P param) {
        E entity = null;
        if (null != param) {
            try {
                entity = this.getEntityClass().newInstance();
                BeanUtils.copyProperties(param, entity);
            } catch (Exception var4) {
                log.error(var4.getMessage());
            }
        }

        return entity;
    }

    protected D entityToDto(E entity) {
        D dto = null;
        if (null != entity) {
            try {
                dto = this.getDtoClass().newInstance();
                BeanUtils.copyProperties(entity, dto);
            } catch (Exception var4) {
                log.error(var4.getMessage());
            }
        }

        return dto;
    }

    protected List<E> paramToEntity(List<P> paramList) {
        List<E> entityList = Lists.newLinkedList();
        if (null != paramList && paramList.size() > 0) {
            paramList.forEach((p) -> {
                E entity = this.paramToEntity(p);
                entityList.add(entity);
            });
        }

        return entityList;
    }

    protected List<D> entityToDto(List<E> entityList) {
        List<D> dtoList = Lists.newLinkedList();
        if (null != entityList && entityList.size() > 0) {
            entityList.forEach((e) -> {
                D dto = this.entityToDto(e);
                dtoList.add(dto);
            });
        }

        return dtoList;
    }

    private Class<E> getEntityClass() {
        ParameterizedType parameterizedType = (ParameterizedType)this.getClass().getGenericSuperclass();
        Type entityType = parameterizedType.getActualTypeArguments()[0];
        return (Class)entityType;
    }

    private Class<D> getDtoClass() {
        ParameterizedType parameterizedType = (ParameterizedType)this.getClass().getGenericSuperclass();
        Type dtoType = parameterizedType.getActualTypeArguments()[1];
        return (Class)dtoType;
    }

    protected QueryWrapper getQueryWrapperByPageParam(PagePARAM pagePARAM){
        QueryWrapper qw = new QueryWrapper();
        List<OrderPARAM> orderParams = pagePARAM.getOrderParams();
        List<SearchPARAM> searchParams = pagePARAM.getSearchPARAMS();

        if(CollectionUtils.isNotEmpty(orderParams)){
            //处理排序
            orderParams.forEach(orderPARAM ->
            {
                if(orderPARAM.getOrder().equals("asc")){
                    qw.orderByAsc(orderPARAM.getFiledName());
                    return;
                }
                if(orderPARAM.getOrder().equals("desc")){
                    qw.orderByAsc(orderPARAM.getFiledName());
                    return;
                }
                Assert.isTrue(false,"排序字段order请求错误");
            });
        }
        if(CollectionUtils.isNotEmpty(searchParams)){
            //处理条件查询
            searchParams.forEach(searchPARAM -> {
                if(searchPARAM.getOperate().equals("eq")){
                    qw.eq(searchPARAM.getFiledName(),searchPARAM.getValue());
                    return;
                }
                if(searchPARAM.getOperate().equals("like")){
                    qw.like(searchPARAM.getFiledName(),searchPARAM.getValue());
                    return;
                }
                if(searchPARAM.getOperate().equals("gt")){
                    qw.gt(searchPARAM.getFiledName(),searchPARAM.getValue());
                    return;
                }
                if(searchPARAM.getOperate().equals("ge")){
                    qw.ge(searchPARAM.getFiledName(),searchPARAM.getValue());
                    return;
                }
                if(searchPARAM.getOperate().equals("le")){
                    qw.le(searchPARAM.getFiledName(),searchPARAM.getValue());
                    return;
                }
                if(searchPARAM.getOperate().equals("lt")){
                    qw.lt(searchPARAM.getFiledName(),searchPARAM.getValue());
                    return;
                }
                Assert.isTrue(false,"条件字段Operate请求错误");
            });
        }
        return qw;
    }

    protected Page<D> entityToDto(Page<E> entityPage) {
        Page<D> dtoPage = new Page<D>();
        if (null != entityPage) {
            try {
                BeanUtils.copyProperties(entityPage, dtoPage);
            } catch (Exception var4) {
                log.error(var4.getMessage());
            }
        }
        return dtoPage;
    }

}
